<?php

/**
 * mailing_group actions.
 *
 * @package    Terapia Vital
 * @subpackage mailing_group
 * @author     Foxsis
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mailing_groupActions extends sfActionsFoxsis
{
	public function executeIndex(sfWebRequest $request)
	{
		return $this->forward('mailing_group', 'list');
	}

	public function executeGrid()
	{
		//Recupero todas as informações necessárias para trabalhar com o grid
		$this->getRequestDataTable();
		$this->data					= MailingGroupTable::consultar($this->filter, $this->order);
		$this->total_data			= count($this->data);
		$this->data					= $this->paginadorDataTable($this->data, $this->page_num, parent::LIMIT_PAGE_DATA_TABLE);

		sfLoader::loadHelpers(array('Url', 'Tag'));

		foreach($this->data as $key => &$r)
		{
			$actions = array(
				'edit'	 => link_to(
	    			' ',
			url_for($this->getModuleName().'/edit?id_mailing_group='.$r['id_mailing_group']),
			array( 'class' => 'icoEditar' )
			),
				'delete' => link_to(
	    			' ',
			url_for($this->getModuleName().'/delete?id_mailing_group='.$r['id_mailing_group']),
			array( 'class' => 'icoExcluir delete' )
			)
			);
			$r['act'] = implode(' ', $actions);
		}

		$this->setJSONDataTable($this->data, $this->colunas, $this->total_data);
		return sfView::NONE;
	}


	public function executeList()
	{
		$this->form = new MailingGroupForm();
		$this->colunas	= array(
		$this->form['id_mailing_group']->renderLabel() => "id_mailing_group",
		$this->form['nm_mailing_group']->renderLabel() => "nm_mailing_group",
			"Ações"	=> "act"
			);

			$this->filter = $this->_getFilter();

			$this->setTemplate('index');
	}

	public function executeFilter()
	{
		$filter = new Filtro(
		$this->getRequestParameter(sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$filter->setCulture($this->getUser()->getCulture());
		$this->getUser()->setAttribute('pg_num',1,$this->getModuleName());

		$this->getUser()->setAttribute(
		sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE,
		serialize($filter),
		$this->getModuleName()
		);

		return $this->forward($this->getModuleName(), '/list');
	}

	public function executeShow(sfWebRequest $request)
	{
		$this->mailing_group = Doctrine::getTable('MailingGroup')->find(array($request->getParameter('id_mailing_group')));
		$this->forward404Unless($this->mailing_group);
	}

	public function executeNew(sfWebRequest $request)
	{
		$this->form = new MailingGroupForm();
	}

	public function executeCreate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new MailingGroupForm();

		$this->processForm($request, $this->form);

		$this->forward($this->getModuleName(), 'new');
	}

	public function validateCreate()
	{
		return true;
	}

	public function handleErrorCreate()
	{
		return $this->forward($this->getModuleName(), 'new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($mailing_group = Doctrine::getTable('MailingGroup')->find(array($request->getParameter('id_mailing_group'))), sprintf('Object mailing_group does not exist (%s).', $request->getParameter('id_mailing_group')));
		$this->form = new MailingGroupForm($mailing_group);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($mailing_group = Doctrine::getTable('MailingGroup')->find(array($request->getParameter('id_mailing_group'))), sprintf('Object mailing_group does not exist (%s).', $request->getParameter('id_mailing_group')));
		$this->form = new MailingGroupForm($mailing_group);

		$this->processForm($request, $this->form);

		$this->forward($this->getModuleName(), 'edit');
	}

	public function validateUpdate()
	{
		return true;
	}

	public function handleErrorUpdate()
	{
		return $this->forward($this->getModuleName(), 'edit');
	}

	public function executeDelete(sfWebRequest $request)
	{
		try
		{
			$request->checkCSRFProtection();

			$this->forward404Unless($mailing_group = Doctrine::getTable('MailingGroup')->find(array($request->getParameter('id_mailing_group'))), sprintf('Object mailing_group does not exist (%s).', $request->getParameter('id_mailing_group')));
			$mailing_group->delete();

			$this->redirect('mailing_group/index');
		}
		catch(Excepiton $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
			$this->redirect('mailing_group/index');
		}

	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		try
		{
			$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
			if ($form->isValid())
			{
				$mailing_group = $form->save();
				$this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
				$this->redirect('mailing_group/edit?id_mailing_group='.$mailing_group->getIdMailingGroup());
			}
			else
			{
				throw new Exception($form->getErrorSchema());
			}
		}
		catch(Exception $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
		}
	}
	
	public function executeDialog()
	{
		$this->form = new MailingGroupForm();
	}
	
	/**
	 * Método usado para savar o grupo pelo dialog
	 * @param sfWebRequest $request
	 */
	public function executeSave(sfWebRequest $request)
	{
		try {
			$formData = new FormData($this->getRequestParameter('mailing_group'));
			$formData->setCulture($this->getUser()->getCulture());
	
			$MailingGroup = new MailingGroup();
			$MailingGroup->set('nm_mailing_group', $formData->get('nm_mailing_group'));
			$MailingGroup->save();
	
			$result = array(
				'id_mailing_group' => $MailingGroup->getIdMailingGroup(),
				'nm_mailing_group' => $MailingGroup->getNmMailingGroup()
			);
		} catch (Exception $e) {
			$result = array(
				'error' => utf8_decode($e->getMessage())
			);
		}

		$this->getResponse()->setHttpHeader('Content-type','application/json; charset=utf-8');
		return $this->renderText(json_encode($result));
	}
}
